package com.sanno.maintain.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.sanno.common.entity.SccMedicalReport;
import com.sanno.common.result.Result;
import com.sanno.common.result.ResultCodeEnum;
import com.sanno.maintain.dao.Maintain1Dao;
import com.sanno.maintain.service.Maintain1Service;
import com.sanno.maintain.vo.Maintain1Vo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/maintain/SccMedicalReport")
@Api(tags = "系统维护-系统设置")
@CrossOrigin
public class Maintain1Controller {

    @Resource
    private Maintain1Service maintain1Service;

    @Resource
    private Maintain1Dao maintain1Dao;

    @RequestMapping(value = "querySccMedicalReport",method = RequestMethod.GET)
    @ApiOperation(value = "条件查询",notes = "",httpMethod = "GET")
    public Result querySccMedicalReport(String searchParams,int page,int limit) throws Exception{
        ObjectMapper objectMapper = new ObjectMapper();
        Maintain1Vo maintain1Vo = new Maintain1Vo();
        if(searchParams != null){
            maintain1Vo = objectMapper.readValue(searchParams,Maintain1Vo.class);
        }
        if(maintain1Vo.getMedicalName() == null){
            maintain1Vo.setMedicalName("");
        }
        QueryWrapper<SccMedicalReport> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("Medical_name",maintain1Vo.getMedicalName());
        queryWrapper.like("status",1);
        Page<SccMedicalReport> newPage = new Page(page,limit);
        Page<SccMedicalReport> SccMedicalReportPage = maintain1Dao.selectPage(newPage, queryWrapper);
        List<SccMedicalReport> records = SccMedicalReportPage.getRecords();
        return Result.ok(records,SccMedicalReportPage.getTotal());
    }

    @RequestMapping(value = "{id}",method = RequestMethod.POST)
    @ApiOperation(value = "id删除",notes = "",httpMethod = "POST")
    public Result delSccMedicalReport(@PathVariable("id")int id){
        SccMedicalReport sccMedicalReport = new SccMedicalReport();
        sccMedicalReport.setStatus(0);
        UpdateWrapper<SccMedicalReport> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("Report_id",id);
        boolean isOk = maintain1Service.update(sccMedicalReport,updateWrapper);
        return Result.build(isOk, ResultCodeEnum.SUCCESS);
    }

    @RequestMapping(value = "delSccMedicalReport",method = RequestMethod.POST)
    @ApiOperation(value = "ids删除",notes = "",httpMethod = "POST")
    public Result delSccMedicalReport(String ids){
        String newIds = ids.substring(0,ids.lastIndexOf(","));
        String idArr[] = newIds.split(",");
        SccMedicalReport sccMedicalReport = new SccMedicalReport();
        sccMedicalReport.setStatus(0);
        UpdateWrapper<SccMedicalReport> updateWrapper = new UpdateWrapper<>();
        updateWrapper.in("Report_id",idArr);
        boolean isOk = maintain1Service.update(sccMedicalReport,updateWrapper);
        return Result.build(isOk,ResultCodeEnum.SUCCESS);
    }

    @RequestMapping(value = "addSccMedicalReport",method = RequestMethod.POST)
    @ApiOperation(value = "添加",notes = "",httpMethod = "POST")
    public Result addSccMedicalReport(SccMedicalReport sccMedicalReport){
        boolean idOk = maintain1Service.save(sccMedicalReport);
        return Result.build(idOk,ResultCodeEnum.SUCCESS);
    }

    @RequestMapping(value = "updateSccMedicalReport",method = RequestMethod.POST)
    @ApiOperation(value = "修改",notes = "",httpMethod = "POST")
    public Result updateSccMedicalReport(SccMedicalReport sccMedicalReport){
        boolean isOk = maintain1Service.updateById(sccMedicalReport);
        return Result.build(isOk,ResultCodeEnum.SUCCESS);
    }

}
